//----
// PART II - Trends in Flows Abroad


	/* This file uses Schedule K-1 data to describe 
	trends in aggregate flows abroad. It also produces
	Figure 1 and Figure 2 in the paper. */


// ----------------
// 1. Calculate Net Income by destination
	
	// Load data
	
	use ${data_dir}/income_table.dta, clear
		
	// exclude income flowing to higher-tier partnerships
	
	drop if class_entity == "P"
	
		/* Entity types include:
			"P" Partnership
			"H" Individual/Household
			"C" Corporation, not S-corporation
			"S" S corporation
			"Y" Limited Liability Entity, most likely taxed as Corporation for US tax purposes
			"T" Trust and Estate
			"D" Custodial Accounts
			"N" Nonprofit/Exempt/Government Entity
			"Z" Entity of unknown type
			"" Unknown */
	
	// collapse to get sums of net income by destination and year
	
	gcollapse (sum) net_income, by(dest year)
		
		/* net_income as defined in the paper and replication readme */
		
	// generate year variable for graph
	
	g y = 2005-1+_n 
		replace y = . if y>2019
	
	replace net_income = net_income/1e9
		
	// Caclulate net income by destination
		
		/* Destinations are:
			"Cay" Cayman Islands
			"Zer" Zero-Rate Havens
			"Hav" Other Havens
			"Con" Conduit
			"For" Other identifiable foreign
			"Und" Undisclosed foreign
			"US"  USA
			"U"   Unknown */
		
	local d_list "Cay Zer Hav Con For Und"
	tokenize "`d_list'"
	local nn: word count `d_list'
	local t = 2005
	forvalues xx = 1/`nn' {
		sum net_income if year==2005 & dest=="``xx''"
		g ``xx'' = r(mean) if y==2005
		}
	local t = 2006
	forvalues xx = 1/`nn' {
		forvalues y = `t'/2019 {
			sum net_income if year==`y' & dest=="``xx''"
			replace ``xx'' = r(mean) if y==`y'
			}		
		}
	
	
	//-------
	// Figure 1

	// clean
	
	keep y-Und
	
	// Grayscale	
		
	graph bar Cay Zer Hav Con For Und, ///
		over(y, gap(10) label(labsize(vsmall))) ///
		stack ///
		bar(1, color(gray*2)) ///
		bar(2, color(gray*1.3)) ///
		bar(3, color(gray*1)) ///
		bar(4, color(gray*.75)) ///
		bar(5, color(gray*.4)) ///
		bar(6, color(gray*.25)) ///
		plotregion(lcolor(black)) ///
		graphregion(fcolor(white) lcolor(white)) ///
		bgcolor(white) ///
		ytitle("Billions of 2020 Dollars", size(msmall) height(4)) ///
		ylabel(0(100)300, labsize(msmall)) ///
		leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "Zero Rate Havens" 3 "Other Havens" 4 "Conduit Countries" 5 "Other Countries" 6 "Undisclosed Foreign") size(small) region(lcolor(white)))
		graph export ${fig_dir}/fig_main_inc_by_dest.png, replace width(1000)

	
	// Color 
	
	graph bar Cay Zer Hav Con For Und, ///
		over(y, gap(10) label(labsize(vsmall))) ///
		stack ///
		bar(1, color("133 41 40")) ///
		bar(2, color("237 144 90")) ///
		bar(3, color("232 202 90")) ///
		bar(4, color("121 168 108")) ///
		bar(5, color(navy*.8)) ///
		bar(6, color(gray*.8)) ///
		plotregion(lcolor(black)) ///
		graphregion(fcolor(white) lcolor(white)) ///
		bgcolor(white) ///
		ytitle("Billions of 2020 Dollars", size(msmall) height(4)) ///
		ylabel(0(100)300, labsize(msmall)) ///
		leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "Zero Rate Havens" 3 "Other Havens" 4 "Conduit Countries" 5 "Other Countries" 6 "Undisclosed Foreign") size(small) region(lcolor(white)))
		graph export ${fig_dir}/fig_main_inc_by_dest_color.png, replace width(1000)
	
	
	// Export
	
	foreach v of varlist Cay-Und {
		replace `v'= round(`v',.1)
		}
	
	export delimited using ${out_dir}/fig1_tab.csv, replace



// ----------------
// Calculate shares of partnership net income by destination
	
	use ${data_dir}/income_table.dta, clear
	
	// exclude income flowing to higher-tier partnerships
	
	drop if class_entity == "P"		
	
	// collapse to get sums of net income by destination and year
	
	gcollapse (sum) net_income, by(dest year)
			
	// generate shares
	
	egen sumtot = sum(net_income), by(year)
	g sharetot = net_income/sumtot*100

	// Combine havens		
	
	g old = dest // temporary holder variable
	replace dest = "Hav" if old=="Con" | old=="Zer" // conduits + zero rates
	replace dest = "For" if old=="Und" // other foreign
	gcollapse (sum)  sharetot, by(dest year)
	
	// Caclulate shares of income by destination
	
	g y = 2005-1+_n 
		replace y = . if y>2019
	
	local d_list "Cay Hav For"
	tokenize "`d_list'"
	local nn: word count `d_list'
	local t = 2005
	forvalues xx = 1/`nn' {
		sum sharetot if year==2005 & dest=="``xx''"
		g ``xx'' = r(mean) if y==2005
		}
	local t = 2006
	forvalues xx = 1/`nn' {
		forvalues y = `t'/2019 {
			sum sharetot if year==`y' & dest=="``xx''"
			replace ``xx'' = r(mean) if y==`y'
			}		
		}
	
	// Stack the lines
	
	replace Hav = Hav + Cay
	replace For = Hav + For
	
	//-------
	// Figure 2

	// clean
	
	keep y-For
	
	// Grayscale
	
	twoway ///
		(line Cay y, lwidth(thick) color(gray*2)) ///
		(line Hav y, lwidth(thick) color(gray*1)) ///
		(line For y, lwidth(thick) color(gray*.5)), ///
		plotregion(lcolor(black)) ///
		graphregion(fcolor(white) lcolor(white)) ///
		bgcolor(white) ///
		ytitle("Share of Partnership Income (%)", height(4) size(msmall)) ///
		xtitle("") ///
		ylabel(0(5)21, labsize(msmall)) ///
		xlabel(2005(2)2019, labsize(msmall)) ///
		leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "All Havens" 3 "All Foreign")  size(msmall) region(lcolor(white)))
		
		graph export ${fig_dir}/fig_main_share_by_dest.png, replace width(1000)	
	
	// Color
	
	twoway ///
		(line Cay y, lwidth(thick) color("133 41 40")) ///
		(line Hav y, lwidth(thick) color("209 145 48")) ///
		(line For y, lwidth(thick) color("30 122 150")), ///
		plotregion(lcolor(black)) ///
		graphregion(fcolor(white) lcolor(white)) ///
		bgcolor(white) ///
		ytitle("Share of Partnership Income (%)", height(4) size(msmall)) ///
		xtitle("") ///
		ylabel(0(5)21, labsize(msmall)) ///
		xlabel(2005(2)2019, labsize(msmall)) ///
		leg(pos(6) cols(3) order (1 "Cayman Islands" 2 "All Havens" 3 "All Foreign")  size(msmall) region(lcolor(white)))
		graph export ${fig_dir}/fig_main_share_by_dest_color.png, replace	width(1000)	
		

	// Export

	foreach v of varlist Cay-For {
		replace `v'= round(`v',.1)
		}
	
	export delimited using ${out_dir}/fig2_tab.csv, replace
		

	
